package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.ImageUtils;
import com.ruoyi.web.core.config.MinioConfig;
import com.ruoyi.web.domain.MinioBaseFile;
import com.ruoyi.web.domain.MinioFile;
import com.ruoyi.web.mapper.MinioFileMapper;
import com.ruoyi.web.service.MinioBaseFileService;
import com.ruoyi.web.service.MinioFileService;
import com.ruoyi.web.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * @author 11610
 * @description 针对表【file】的数据库操作Service实现
 * @createDate 2025-04-04 22:14:07
 */
@Service
public class MinioFileServiceImpl extends ServiceImpl<MinioFileMapper, MinioFile>
        implements MinioFileService {

    @Autowired
    private MinioUtil minioUtil;

    @Resource
    private MinioConfig minioConfig;

    @Autowired
    private MinioBaseFileService minioBaseFileService;

    @Override
    public MinioFile saveFile(String fileName, String md5) {
        MinioFile minioFile = new MinioFile();
        minioFile.create();
        minioFile.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        minioFile.setFileName(fileName);
        minioFile.setFileInfoId(md5);
        saveOrUpdate(minioFile);
        return minioFile;
    }

    @Override
    public AjaxResult uploadReport(MultipartFile file, String md5) {
        try {
            String saveFileName;
            if (file != null) {
                // 获取到上传的文件名
                String fileName = file.getOriginalFilename();
                String suffix = FileUtils.getNameSuffix(fileName);
                if (minioConfig.isImgThumb()) {
                    //压缩图片
                    if (ImageUtils.isPicture(suffix)) {
                        MultipartFile multipartFile = ImageUtils.thumbnailsPic(file);
                        String originalFilename = multipartFile.getOriginalFilename();
                        String newMd5 = md5 + FileUtils.THUMBN;
                        saveFileName = FileUtils.getSaveFileNameT(fileName, md5);
                        // 上传文件
                        minioUtil.upload(multipartFile, saveFileName);
                        // 保存文件基础信息
                        minioBaseFileService.saveFileInfo(newMd5, saveFileName, originalFilename, multipartFile.getSize() + "");
                    }
                }
                saveFileName = FileUtils.getSaveFileName(fileName, md5);
                // 上传文件
                minioUtil.upload(file, saveFileName);
                // 保存文件基础信息
                minioBaseFileService.saveFileInfo(md5, saveFileName, fileName, file.getSize() + "");
            }
            // 保存文件信息
            MinioFile daiWeiBaiFile1 = this.saveFile(file.getOriginalFilename(), md5);
            daiWeiBaiFile1.setOriginalFilename(file.getOriginalFilename());
            return AjaxResult.success(daiWeiBaiFile1);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("文件上传错误");
        }
    }

    @Override
    public AjaxResult checkFile(String md5, String fileName) {
        if (minioConfig.isFastUpload()) {
            MinioBaseFile minioBaseFile = minioBaseFileService.getById(md5);
            if (minioBaseFile != null) {
                // 保存文件信息
                MinioFile daiWeiBaiFile = this.saveFile(fileName, md5);
                daiWeiBaiFile.setOriginalFilename(minioBaseFile.getFileName());
                return AjaxResult.success(daiWeiBaiFile);
            }
        }
        return AjaxResult.success();
    }

    @Override
    public void download(String saveFileName, HttpServletResponse response, HttpServletRequest request) throws Exception {
        minioUtil.download(request,response, saveFileName);
    }

    @Override
    public AjaxResult deleteById(String fileId) {
        MinioFile byId = this.getById(fileId);
        boolean b = this.removeById(fileId);
        if (b) {
            String fileInfoId = byId.getFileInfoId();
            LambdaQueryWrapper<MinioFile> fileLambdaQueryWrapper = new LambdaQueryWrapper<>();
            fileLambdaQueryWrapper.eq(MinioFile::getFileInfoId, fileInfoId);
            Long aLong = this.getBaseMapper().selectCount(fileLambdaQueryWrapper);
            if (aLong == 0) {
                MinioBaseFile byId1 = minioBaseFileService.getById(fileInfoId);
                minioBaseFileService.removeById(fileInfoId);
                minioBaseFileService.removeById(fileInfoId + FileUtils.THUMBN);
                minioUtil.delete(FileUtils.getSaveFileName(byId1.getFileName(), fileInfoId));
                minioUtil.delete(FileUtils.getSaveFileNameT(byId1.getFileName(), fileInfoId));
            }
        }
        return AjaxResult.success();
    }

    @Transactional
    @Override
    public AjaxResult deleteByIds(List<String> fileIds) {
        fileIds.forEach(this::deleteById);
        return AjaxResult.success();
    }
}




